﻿namespace SecurityGroupsAdministration
{
    using System.Collections.Generic;

    using Microsoft.SharePoint;

    public class SecurityGroupsBL
    {
        #region Fields

        List<DepartmentSecurityGroup> _DepartmentSecurityGroups;
        List<StoreSecurityGroupsInformation> _SecurityGroups;

        #endregion Fields

        #region Properties

        public List<DepartmentSecurityGroup> DepartmentSecurityGroups
        {
            get
            {
                if ( _DepartmentSecurityGroups == null )
                    _DepartmentSecurityGroups = new List<DepartmentSecurityGroup>( );
                return _DepartmentSecurityGroups;
            }
        }

        public List<StoreSecurityGroupsInformation> SecurityGroups
        {
            get
            {
                if ( _SecurityGroups == null )
                    _SecurityGroups = new List<StoreSecurityGroupsInformation>( );
                return _SecurityGroups;
            }
        }

        #endregion Properties

        #region Methods

        public void DeleteSelectedGroup( string siteURL, string departmentName, string securityGroup )
        {
            SPSite storeSite = new SPSite( siteURL );
            try
            {

                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {

                    SPList departmentFieldsView = storeWeb.Lists[ "Department Store Assignment" ];
                    SPQuery sourceUserDepartments = new SPQuery( );
                    sourceUserDepartments.Query = "<Where><And><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + departmentName + "</Value></Eq><Eq><FieldRef Name='Store_x0020_Security_x0020_Group' /><Value Type='Text'>" + securityGroup + "</Value></Eq></And></Where>";

                    SPListItemCollection allFieldViews = departmentFieldsView.GetItems( sourceUserDepartments );
                    if ( allFieldViews.Count == 0 )
                        return;
                    allFieldViews[ 0 ].Delete( );

                }
            }
            catch
            {

            }
            finally
            {
                storeSite.Dispose( );
            }
        }

        public bool IsFieldSelected( string siteURL, string departmentName, string securityGroup )
        {
            SPSite storeSite = new SPSite( siteURL );
            bool results = false;
            using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
            {

                SPList secGroups = storeWeb.Lists[ "Department Store Assignment" ];
                SPQuery sourceUserDepartments = new SPQuery( );

                sourceUserDepartments.Query = "<Where><And><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + departmentName + "</Value></Eq><Eq><FieldRef Name='Store_x0020_Security_x0020_Group' /><Value Type='Text'>" + securityGroup + "</Value></Eq></And></Where>";

                SPListItemCollection allFieldViews = secGroups.GetItems( sourceUserDepartments );
                if ( allFieldViews.Count > 0 )
                    results = true;

            }
            return results;
        }

        public void LoadSecurityGroups( string siteURL, string departmentName )
        {
            SPSite storeSite = new SPSite( siteURL );
            try
            {

                StoreSecurityGroupsInformation singleGroup = new StoreSecurityGroupsInformation( );
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {

                    SPList departmentListValues = storeWeb.Lists[ "Store Security Groups" ];

                    SPQuery orderSecGroup = new SPQuery( );
                    orderSecGroup.Query = "<OrderBy><FieldRef Name='Security_x0020_Group' Ascending='True' /></OrderBy>";

                    SPListItemCollection allDepts = departmentListValues.GetItems( orderSecGroup );

                    foreach ( SPListItem item in allDepts )
                    {
                        singleGroup = new StoreSecurityGroupsInformation( );

                        if ( item[ "Security Group" ] != null )
                            singleGroup.SecurityGroup = item[ "Security Group" ].ToString( );

                        if ( !IsFieldSelected( siteURL, departmentName, singleGroup.SecurityGroup ) )
                            SecurityGroups.Add( singleGroup );

                    }
                }
            }
            catch
            {

            }
            finally
            {
                storeSite.Dispose( );
            }
        }

        public void LoadSelectedSecurityGroups( string siteURL, string departmentName )
        {
            SPSite storeSite = new SPSite( siteURL );

            try
            {

                DepartmentSecurityGroup singleGroup = new DepartmentSecurityGroup( );
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {

                    SPList departmentListValues = storeWeb.Lists[ "Department Store Assignment" ];
                    SPQuery selectedSecurityGroup = new SPQuery( );

                    selectedSecurityGroup.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + departmentName + "</Value></Eq></Where>";

                    SPListItemCollection selectedGroups = departmentListValues.GetItems( selectedSecurityGroup );
                    foreach ( SPListItem item in selectedGroups )
                    {
                        singleGroup = new DepartmentSecurityGroup( );

                        if ( item[ "Store Security Group" ] != null )
                            singleGroup.SecurityGroup = item[ "Store Security Group" ].ToString( );
                        if ( item[ "Department Name" ] != null )
                            singleGroup.DepartmentName = item[ "Department Name" ].ToString( );

                        DepartmentSecurityGroups.Add( singleGroup );

                    }
                }
            }
            catch
            {
            }
            finally
            {
                storeSite.Dispose( );
            }
        }

        public void SaveSelectedGroup( string siteURL, string departmentName, string securityGroup )
        {
            SPSite storeSite = new SPSite( siteURL );
            try
            {

                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPListItem item;
                    SPList departmentFieldsView = storeWeb.Lists[ "Department Store Assignment" ];
                    item = departmentFieldsView.Items.Add( );
                    item[ "Title" ] = departmentName;
                    item[ "Department Name" ] = departmentName;
                    item[ "Store Security Group" ] = securityGroup;

                    item.Update( );
                    departmentFieldsView.Update( );

                }
            }
            catch
            {

            }
            finally
            {
                storeSite.Dispose( );
            }
        }

        #endregion Methods
    }
}